package com.hsit.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSON;
import com.hsit.dao.EasyUiTree;
import com.hsit.dao.ExtTree;
import com.hsit.service.TreeService;

@Controller
@RequestMapping(value = "/Tree")
public class TreeController {

	@Autowired
	private TreeService treeService;

	@RequestMapping(value = "/json3")
	public void TreeTest(HttpServletResponse response,
			HttpServletRequest request) throws IOException {
		String str = request.getParameter("id");
		if ("".equals(str) || str == null) {
			// idΪ�վͲ鸸��
			List list=treeService.parentList();
		String json=	JSON.toJSONString(list);
			response.getWriter().write(json);
		} else {
            
			int id = Integer.parseInt(str);
			List<EasyUiTree> list=treeService.childrenList(id);
			String json=JSON.toJSONString(list);
			response.getWriter().write(json);
			
		}

	}
	
	
	@RequestMapping(value = "/json4")
	public void TreeTest2(HttpServletResponse response,
			HttpServletRequest request) throws IOException {
		String str = request.getParameter("id");
		if ("root".equals(str)) {
			// idΪ�վͲ鸸��
			List list=treeService.parentList2();
		String json=	JSON.toJSONString(list);
			response.getWriter().write(json);
		} else {
            
			int id = Integer.parseInt(str);
			List<ExtTree> list=treeService.childrenList2(id);
			String json=JSON.toJSONString(list);
			response.getWriter().write(json);
			
		}

	}
	
	@RequestMapping(value = "/json5")
	public void TreeTest5(HttpServletResponse response,
			HttpServletRequest request) throws IOException {
		String str = request.getParameter("id");
		if ("root".equals(str)) {
			// idΪ�վͲ鸸��
			List list=treeService.parentList2();
		String json=	JSON.toJSONString(list);
			response.getWriter().write(json);
		} else {
            
			int id = Integer.parseInt(str);
			List<ExtTree> list=treeService.childrenList2(id);
			String json=JSON.toJSONString(list);
			response.getWriter().write(json);
			
		}

	}
	
	@RequestMapping(value="/pp")
	public void pp(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String start=request.getParameter("start");
		String limit=request.getParameter("limit");
		List list=treeService.pp2();
		Map map=new HashMap();
		map.put("items", list);
		String json=JSON.toJSONString(map);
		response.getWriter().write(json);
	}
	
	@RequestMapping(value="/pp2")
	public void pp2(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String idstr=request.getParameter("id");
	    if (!"".equals(idstr)&&idstr!=null) {
	    	int id=Integer.parseInt(idstr);
			List list=treeService.pp3(id);
			Map map=new HashMap();
			map.put("items", list);
			String json=JSON.toJSONString(map);
			response.getWriter().write(json);
		}
	}
	
	@RequestMapping(value="/formGet")
	public void formGet(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="photopath1") CommonsMultipartFile file) throws IOException{
		String date=request.getParameter("date");
		String a=file.getName();
		InputStream ip=file.getInputStream();
		OutputStream op=new FileOutputStream(new File("D:/a.jpg"));
		byte[] b=new byte[ip.available()];
		ip.read(b);
		op.write(b);
		ip.close();
		op.close();
	}

}
